﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerQuanLyHopDongThinhGiang: IQuanLyHopDongThinhGiang
    {
        public QuanLyHopDongThinhGiang GetQuanLyHopDongThinhGiang(int oid)
        {
            string sql = @" Select NamHoc, HocKy
                            From QuanLyHopDong
                            Where Oid = @Oid";
            SqlParameter param = new SqlParameter("@Oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            QuanLyHopDongThinhGiang quanLyHopDongThinhGiang = new QuanLyHopDongThinhGiang();

            quanLyHopDongThinhGiang.Oid = oid;
            int id;
            if (int.TryParse(row["NamHoc"].ToString(), out id))
            {
                SqlServerNamHoc sqlServerNamHoc = new SqlServerNamHoc();
                quanLyHopDongThinhGiang.NamHoc = sqlServerNamHoc.GetNamHoc(id);
            }
            if (int.TryParse(row["HocKy"].ToString(), out id))
            {
                SqlServerHocKy sqlServerHocKy = new SqlServerHocKy();
                quanLyHopDongThinhGiang.HocKy = sqlServerHocKy.GetHocKy(id);
            }
            return quanLyHopDongThinhGiang;
        }

        public List<QuanLyHopDongThinhGiang> GetListQuanLyHopDongThinhGiang()
        {
            string sql = @" Select Oid, NamHoc, HocKy
                            From QuanLyHopDong";

            DataTable dt = DataProvider.GetDataTable(sql);
            List<QuanLyHopDongThinhGiang> list = new List<QuanLyHopDongThinhGiang>();

            foreach (DataRow row in dt.Rows)
            {
                QuanLyHopDongThinhGiang quanLyHopDongThinhGiang = new QuanLyHopDongThinhGiang();

                quanLyHopDongThinhGiang.Oid = int.Parse(row["Oid"].ToString());
                int id;
                if (int.TryParse(row["NamHoc"].ToString(), out id))
                {
                    SqlServerNamHoc sqlServerNamHoc = new SqlServerNamHoc();
                    quanLyHopDongThinhGiang.NamHoc = sqlServerNamHoc.GetNamHoc(id);
                }
                if (int.TryParse(row["HocKy"].ToString(), out id))
                {
                    SqlServerHocKy sqlServerHocKy = new SqlServerHocKy();
                    quanLyHopDongThinhGiang.HocKy = sqlServerHocKy.GetHocKy(id);
                }
                list.Add(quanLyHopDongThinhGiang);
            }
            return list;
        }

        public int Insert(QuanLyHopDongThinhGiang quanLyHopDongThinhGiang)
        {
            string sql = @" Insert into QuanLyHopDong(NamHoc, HocKy)
                            Values(@NamHoc, @HocKy)";
            SqlParameter[] param = {
                                       new SqlParameter("@NamHoc", quanLyHopDongThinhGiang.NamHoc.Oid),
                                       new SqlParameter("@HocKy", quanLyHopDongThinhGiang.HocKy.Oid)
                                   };
            DataProvider.Insert(sql, param);
            return 0;
        }

        public int Update(QuanLyHopDongThinhGiang quanLyHopDongThinhGiang)
        {
            string sql = @" Update QuanLyHopDong
                            Set NamHoc = @NamHoc, 
                                HocKy = @HocKy
                            Where Oid = @Oid";
            SqlParameter[] param = {
                                       new SqlParameter("@Oid", quanLyHopDongThinhGiang.Oid),
                                       new SqlParameter("@NamHoc", quanLyHopDongThinhGiang.NamHoc.Oid),
                                       new SqlParameter("@HocKy", quanLyHopDongThinhGiang.HocKy.Oid)
                                   };
            return DataProvider.Update(sql, param);
        }

        public int Delete(QuanLyHopDongThinhGiang quanLyHopDongThinhGiang)
        {
            string sql = @" Delete From QuanLyHopDong Where Oid = @Oid";

            SqlParameter param = new SqlParameter("@Oid", quanLyHopDongThinhGiang.Oid);
            return DataProvider.Update(sql, param);
        }


        public int Save(QuanLyHopDongThinhGiang obj)
        {
            throw new NotImplementedException();
        }
    }
}
